package parser;

import org.w3c.dom.*;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/4/21 11:21
 * Version:1.0-SNAPSHOT
 * Description:
 */
public class DOMParser {

    public static void main(String[] args) {
        // 1.创建DOM解析工厂
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        try {
            // 2.获取解析器
            DocumentBuilder builder = factory.newDocumentBuilder();

            // 3.开始解析
            Document document = builder.parse(DOMParser.class.getClassLoader().getResourceAsStream("students.xml"));

            // 4.根据解析结果进行操作
            // 获取根标签
            Element root = document.getDocumentElement();
            System.out.println("根标签的标签名:"+root.getTagName());

            // 获取属性
            // 1.根据属性名获取属性值
            String no = root.getAttribute("no");
            System.out.println("no属性值:"+no);
            // 2.获取指定元素的所有属性
            // 返回值NamedNodeMap:类似于List的一种集合
            NamedNodeMap attributes = root.getAttributes();
            for(int i = 0; i < attributes.getLength(); i++){
                Node attr = attributes.item(i);
                System.out.println(attr);
            }

            // 获取指定元素的所有子节点
            // NodeList:类似于List的一种集合
            NodeList childNodes = root.getChildNodes();
            System.out.println("子节点数量:"+childNodes.getLength());
            for(int i = 0; i < childNodes.getLength(); i++){
                Node childNode = childNodes.item(i);
                // 判断当前节点类型
                if(childNode.getNodeType() == Node.ELEMENT_NODE){
                    // 元素节点
                    Element element = (Element) childNode;
                    System.out.println("子标签:"+element.getTagName());
                    NamedNodeMap attributes2 = element.getAttributes();
                    for(int j = 0; j < attributes2.getLength(); j++){
                        Node node = attributes2.item(j);
                        System.out.println(node);
                    }
                    NodeList childNodes1 = element.getChildNodes();
                    System.out.println("student的子节点数量:"+childNodes1.getLength());
                    for(int k = 0; k < childNodes1.getLength();k++){
                        Node node2 = childNodes1.item(k);
                        if(node2.getNodeType() == Node.ELEMENT_NODE){
                            Element nameNode = (Element) node2;
                            System.out.println("姓名:"+nameNode.getTextContent());
                        }
                    }
                }
                if(childNode.getNodeType() == Node.TEXT_NODE){
                    // 文本节点
                    System.out.println("文本内容:"+childNode.getTextContent());
                }

            }

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
